package com.ml.mall.service.catalogue.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ml.mall.bean.req.catalogue.CatalogueDelDto;
import com.ml.mall.bean.req.catalogue.SysCatalogueAddDto;
import com.ml.mall.bean.res.catalogue.SysCatalogueQueryRes;
import com.ml.mall.entity.catalogue.SysCatalogue;
import com.ml.mall.entity.sys.SysUser;
import com.ml.mall.enums.CommonEnum;
import com.ml.mall.enums.SysSeqNameEnum;
import com.ml.mall.exception.BizException;
import com.ml.mall.mapper.catalogue.SysCatalogueMapper;
import com.ml.mall.mapper.catalogue.SysCatalogueProductMapper;
import com.ml.mall.security.JwtUtil;
import com.ml.mall.service.catalogue.SysCatalogueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ml.mall.service.common.CommonSeqService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
* <p>
    *  服务实现类
    * </p>
*
* @author pl
* Created 2021-05-25
*/
@Service
@Slf4j
public class SysCatalogueServiceImpl extends ServiceImpl<SysCatalogueMapper, SysCatalogue> implements SysCatalogueService {

    @Autowired
    private  SysCatalogueMapper  sysCatalogueMapper;
    @Autowired
    private SysCatalogueProductMapper sysCatalogueProductMapper;
    @Autowired
    private CommonSeqService commonSeqService;

    /**
     * 实现收藏夹目录添加功能
     * @param dto
     * @return
     */
    @Override
    public int sysCatalogueNew(SysCatalogueAddDto dto) {
        SysUser currentUser = JwtUtil.getCurrentUser();
        Integer catalogueId = Integer.parseInt(String.valueOf(commonSeqService.nextSysOrderSeq(SysSeqNameEnum.SEQ_CATALOGUE_ID.getCode())));
        //创建收藏夹目录
        if(currentUser==null){
            throw new RuntimeException("用户不存在，请重新登录！");
        }
        //如果该账户下已经存在相同的收藏夹目录，不能创建新目录
        QueryWrapper<SysCatalogue> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",currentUser.getCompanyId())
                .eq("catalogue_name",dto.getCatalogueName());
        SysCatalogue selectOne = sysCatalogueMapper.selectOne(queryWrapper);
        //找到相同的就抛异常，没找到就进行添加
        if (selectOne!=null){
            throw new BizException(CommonEnum.ALREADY_EXIST_SAME);
        }
        //判断该账户下是否已经满12个收藏夹
        QueryWrapper<SysCatalogue> queryCountWrapper = new QueryWrapper<>();
        queryCountWrapper.eq("company_id",currentUser.getCompanyId());
        Integer catalogueCount = sysCatalogueMapper.selectCount(queryCountWrapper);
        if (catalogueCount==12){
            throw new BizException(CommonEnum.INSERT_MAX_ERROR);
        }
        //未满12个，创建新的收藏夹
        SysCatalogue sysCatalogue = new SysCatalogue();
        sysCatalogue.setCompanyId(currentUser.getCompanyId())
                .setCatalogueName(dto.getCatalogueName())
                .setCatalogueId(catalogueId)
                .setPrefferedType(dto.getPrefferedType());
        int insert = sysCatalogueMapper.insert(sysCatalogue);
        if (insert==0){
            throw new BizException(CommonEnum.INSERT_ERROR);
        }
        return insert;
    }

    /**
     * 实现收藏夹目录相关信息查询功能
     * @return
     */
    @Override
    public List<SysCatalogueQueryRes> sysCatalogueQuery() {
        //声明相关对象
        List<SysCatalogueQueryRes> listRes = new ArrayList<>();
        //通过jwt获取companyId
        SysUser currentUser = JwtUtil.getCurrentUser();
        Long companyId = currentUser.getCompanyId();
        //进行查询语句配置
        QueryWrapper<SysCatalogue> queryCatalogueWrapper = new QueryWrapper<>();
        queryCatalogueWrapper.eq("company_id",companyId);
        List<SysCatalogue> sysCatalogueList = sysCatalogueMapper.selectList(queryCatalogueWrapper);
        //进行封装
        for (SysCatalogue sysCatalogue:sysCatalogueList){
            SysCatalogueQueryRes res = new SysCatalogueQueryRes();
            res.setCatalogueId(sysCatalogue.getCatalogueId())
                    .setCatalogueName(sysCatalogue.getCatalogueName());
            listRes.add(res);
        }
        return listRes;
    }

    @Override
    public int delCatalog(CatalogueDelDto dto) {
        SysCatalogue sysCatalogue = sysCatalogueMapper.selectById(dto.getId());
        int delNum = sysCatalogueMapper.deleteById(dto.getId());
        if(sysCatalogue == null){
            throw new BizException(CommonEnum.ICATAGORY_DEL_ERR);
        }
        sysCatalogueProductMapper.delCatelogoryProductByCid(sysCatalogue.getCatalogueId());
        return delNum;
    }
}
